package com.sx.core.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lihao1 on 11/28/15.
 */
public class QuerySearch {




    public static void main(String[] args){
        String sql="select * from t_user";
        Map map=new HashMap();
        map.put("Q$name$EQ","dd");
        map.put("Q$a.id$LK","dd");
        System.out.println(where(map,false));
        System.out.println(orderby(map,false));
    }
    static private final String prefix="Q";
    static private final String regex="[_]";
    static private final Log log= LogFactory.getLog(QuerySearch.class);
    public static String where(Map<String,Object> params, boolean haswhere) {
        if(params.isEmpty()){
            return "";
        }
        StringBuilder sql=new StringBuilder();
        if(!haswhere){
            sql.append(" where 1=1 ");
        }
        for(Map.Entry<String,Object> entry:params.entrySet()){
            String key=entry.getKey();
            Object value=entry.getValue();
            if(value==null || "".equals(value.toString())){
                continue;
            }
            setValue(entry,value);
            String as[] = key.split(regex);
        /*Q_title_LK*/
        /*Q_u.title_LK*/
            if (as != null && as.length == 3 && prefix.equals(as[0])) {
                sql.append(getPartSql(as[2],as[1],key));
                if(as[2].equals("LK")){
                    entry.setValue(new StringBuffer().append("%").append(value).append("%").toString());
                }
            }

        }

        return sql.toString();
    }
    public static String orderby(Map<String,Object> params, boolean append){
        StringBuilder orderby=new StringBuilder();
        if(append){
            orderby.append(" order by ");
        }
        StringBuilder orderbyelenment=new StringBuilder();
        for(Map.Entry<String,Object> entry:params.entrySet()){

            String key=entry.getKey();
            Object value=entry.getValue();
            if(value==null){
                continue;
            }
            String as[] = key.split(regex);
        /*Q_title_LK*/
        /*Q_u.title_LK*/
            if(as!=null && as.length==2 && prefix.equals(as[0])){
                orderbyelenment.append(",");
                orderbyelenment.append(as[1]).append(" ").append(value);
            }
        }
        if(orderbyelenment.length()>1){
            orderby.append(orderbyelenment.substring(1));
            return orderby.toString();
        }
        return "";
    }
    private static String getPartSql(String operation, String property, String key) {
        String s = "";
        if ("LT".equals(operation)){
            s = (new StringBuilder()).append(" and ").append(property).append(" < :").append(key).toString();
        } else if ("GT".equals(operation)) {
            s = (new StringBuilder()).append(" and ").append(property).append(" > :").append(key).toString();
        } else if ("LTE".equals(operation)) {
            s = (new StringBuilder()).append(" and ").append(property).append(" <= :").append(key).toString();
        } else if ("GTE".equals(operation)) {
            s = (new StringBuilder()).append(" and ").append(property).append(" >= :").append(key).toString();
        } else if ("LK".equals(operation)) {
            s = (new StringBuilder()).append(" and ").append(property).append(" like :").append(key).toString();
        } else if ("LLK".equals(operation)) {
            s = (new StringBuilder()).append(" and ").append(property).append(" like :").append(key).toString();
        } else if ("RLK".equals(operation)) {
            s = (new StringBuilder()).append(" and ").append(property).append(" like :").append(key).toString();
        } else if ("NULL".equals(operation))
            s = (new StringBuilder()).append(" and ").append(property).append(" is null ").toString();
        else if ("NOTNULL".equals(operation))
            s = (new StringBuilder()).append(" and ").append(property).append(" is not null ").toString();
        else if ("NEQ".equals(operation)){
            s = (new StringBuilder()).append(" and ").append(property).append(" != :").append(key).toString();
        }else if("EQ".equals(operation)){
            s = (new StringBuilder()).append(" and ").append(s).append(property).append(" = :").append(key).toString();
        }
        return s;
    }
    public static void setValue(Map.Entry<String,Object> entry,Object value){
        if(value instanceof Integer ||
                value instanceof Long ||
                value instanceof Character ||
                value instanceof String  ||
                value instanceof Double  ||
                value instanceof Float  ||
                value instanceof BigDecimal ||
                value instanceof Date ||
                value instanceof java.sql.Date ||
                value instanceof Short ){
            return;
        }
        entry.setValue(value.toString());
    }
}
